var Benchmark = require('benchmark');
// var util = require('util');
var suite = new Benchmark.Suite;
const NEW_LINE_SEPARATOR = require('os').EOL;

const consoleProvider = require('./v0.4.1/lib/providers/console_provider')(null,{flushInterval:1000});
const args = ['this is version x'];
/**
 * 
 * 
 * @param {Date} date 
 */
function dateFormat(date) {
    return date.getFullYear()+'-' + (date.getMonth() + 1) + ' '+date.getDate() + ' '+date.getHours()+':'+date.getMinutes() + ':'+date.getSeconds();
}

function myFormat(params) {
    var str = '';
    if (!params) {
        return str;
    }
    var element;
    for (var i=0,len=params.length;i<len;i++) {
        element = params[i];
        if (element instanceof Error) {
            str += element.stack + '\n';
        } else if (typeof(element) === 'object') {
            str += ' '+ JSON.stringify(element);
        } else {
            str += element;
        }
    }
    return str;
}
// var str = '';
// process.stdout._handle.setBlocking(false);
suite

.add('loop',function() {
    var perfix =  '\x1b[32m';
    perfix += dateFormat(new Date());
    perfix+= '['+'debug'+']';
    var len = args.length;
    const params = new Array(len+2);
    params[0] = perfix;
    for (var i=1;i<=len;i++) {
        params[i] = args[i-1];
    }
    params[len+1] = '\x1b[0m';
    // str += util.format.apply(null,params) + NEW_LINE_SEPARATOR;
    str += myFormat(params) + NEW_LINE_SEPARATOR;
    
    //console.info.apply(console,params);
})
.add('console provider',function() {
    consoleProvider.print(args,'debug');
})
// .add('async',function() {
//     alogger = logger41.init({flushInterval:500});
//     alogger.debug('this is version as');
// })
.on('cycle', function(event) {// add listeners
  console.log(String(event.target));
})
.on('complete', function() {
  console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({async:true});

